﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebSimplicity.Data.Helpers;
using System.Web.Mvc;
using System.IO;
using System.Web;

namespace WebSimplicity.Data
{
    public static class Themes
    {
        static Website web = new Website().Current();
        public static IEnumerable<Theme> GetFreeThemes()
        {
            Entities db = new Entities(Config.GetConnection("Data.Model"));
            var themes = db.Themes.Where(i => i.System == true && i.Cost == 0);
            db.Connection.Close();
            return themes.ToList();
        }
        public static string GetThemeScreenshot(Guid ThemeId)
        {
            return string.Format("~/content/themes/{0}/screenshot.png", ShortGuid.Encode(ThemeId));
        }
        public static List<SelectListItem> GetTemplates()
        {
            List<SelectListItem> list = new List<SelectListItem>();
            DirectoryInfo di = new DirectoryInfo(GetThemePath() + "/page_templates");
            foreach (FileInfo fi in di.GetFiles())
            {
                if(fi.Extension ==".cshtml"){
                    string name = fi.Name.Replace(fi.Extension, string.Empty);
                    list.Add(new SelectListItem { Text = name, Value = name, Selected = (name.ToLower() == "index") });
                }
            }
            return list;
        }
        public static string GetThemePath()
        {
            return HttpContext.Current.Server.MapPath("/Themes/" + ShortGuid.Encode(web.Theme)) + "/";
        }
        public static string GetRelativeThemePath()
        {
            return ShortGuid.Encode(web.Theme) + "/";
        }
        public static HtmlString GetThemeCSS(string files)
        {
            return new HtmlString(string.Format("<link type=\"text/css\" href=\"/css/{0}\" rel=\"stylesheet\"/>", files));
        }
        public static HtmlString GetThemeJS(string files)
        {
            return new HtmlString(string.Format("<script src=\"/js/{0}\" type=\"text/javascript\"></script>", files));
        }
    }
}
